package com.sudu.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sudu.domain.*;
import com.sudu.domain.Tag;
import com.sudu.service.CommentService;
import com.sudu.service.UserService;
import com.sudu.service.VideoService;
import com.sudu.service.likeService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@Api
public class VideoController {

    @Autowired
    private VideoService videoService;
    @Autowired
    private UserService userService;
    @Autowired
    private likeService likeService;
    @Autowired
    private CommentService commentService;

    @ApiOperation(value = "获取首页tag分类标签")
    @GetMapping("/home/tag")
    public Result getHomeTag() {
        List<Tag> tags = videoService.fineByAll();
        System.out.println(tags);
        return new Result(Code.GET_OK, tags);

    }


    @ApiOperation(value = "分页获取某栏目数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "tag_id", dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "希望获取的页数", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页渲染视频的个数", dataType = "Integer")
    })
    @RequestMapping("/detail/{id}")
    public Result selectByPage(@PathVariable Integer id, Integer page, Integer pageSize) {
        if (page != 0) {
            page = page * pageSize;
        }
        List<Cover> list = videoService.selectByPage(id, page, pageSize);
        System.out.println(list);
        return new Result(Code.GET_OK, list);
    }

    @ApiOperation(value = "根据vid获取视频信息")
    @GetMapping("/article/{vid}")
    public Result selectByVid(
            @ApiParam(name = "id", value = "视频id", required = true)
            @PathVariable Integer vid) {
        Detail detail = videoService.selectByVid(vid);
        UserInfo userInfo = userService.getUserInfoById(detail.getCreateBy());
        detail.setUserInfo(userInfo);
        detail.setTagTitle(videoService.fineById(detail.getTag()));
        System.out.println(detail);
        return new Result(Code.GET_OK, detail);
    }

    @ApiOperation(value = "根据uid/tag获取推荐视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "Integer"),
            @ApiImplicitParam(name = "tag", value = "tag id", dataType = "Integer"),
    })
    @GetMapping("/commend/{uid}")
    public Result selectCovetByUid(@PathVariable int uid, int tag) {
        List<Cover> covers = videoService.selectCoverByUid(uid);
        if (covers.size() < 6) {
            int len = 6 - covers.size();
            List<Cover> coverList = videoService.selectCoverByTag(tag, len);
            int index = 0;
            while (index < coverList.size()) {
                covers.add(coverList.get(index));
                index++;
                len-- ;
            }
            if(len > 0) {
                coverList = videoService.selectCoverByTag(2, len);
                index = 0;
                while (index < coverList.size()) {
                    covers.add(coverList.get(index));
                    index++ ;
                }
            }

        } else if (covers.size() > 6) {
            covers = covers.subList(0, 6);
        }
        return new Result(Code.GET_OK, covers);
    }

    @ApiOperation(value = "根据 uid 获取up 主信息")
    @GetMapping("/upInfo/{vid}")
    public Result selectUpInfo(@PathVariable int vid) {
        UserInfo userInfo = videoService.selectUpInfoByVid(vid);
        return new Result(Code.GET_OK, userInfo);
    }

    @ApiOperation(value = "进入页面获取是否收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "Integer"),
            @ApiImplicitParam(name = "obj", value = "key:视频id", dataType = "Integer"),
    })
    @GetMapping("/collection/{uid}")
    public Result collection(@PathVariable int uid, int vid) {
        boolean flag ;
        Favorite favorite  =  likeService.IfFavorite(uid, vid) ;
        if (favorite == null) {
            flag = false ;
        } else  {
            flag = favorite.getIsDelete() == 1;
        }
        return new Result(Code.GET_OK, flag);

    }

    @ApiOperation(value = "进入页面获取是否关注")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "Integer"),
            @ApiImplicitParam(name = "obj", value = "key:up主id", dataType = "Integer"),
    })
    @GetMapping("/subAttention/{uid}")
    public Result subAttention(@PathVariable int uid, int subId) {
        boolean flag ;
        Attention attention = likeService.IfAttention(uid, subId) ;
        if (attention == null) {
            flag = false ;
        } else  {
            flag = attention.getIsDelete() == 1;
        }
        return new Result(Code.GET_OK, flag);
    }

    @ApiOperation(value = "我的收藏")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "Integer"),
            @ApiImplicitParam(name = "tagId", value = "栏目id", dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "页数", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页渲染个数", dataType = "Integer"),
    })
    @GetMapping("/myFavorite/{uid}")
    public Result myFavorite(@PathVariable int uid , int tagId , int page , int pageSize) {
        if (page != 0) {
            page = page * pageSize;
        }
        return new Result(Code.GET_OK , videoService.selectCoverByVid(likeService.selectVidByUid(uid),tagId,page,pageSize)) ;
    }

    @ApiOperation(value = "我的发布")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", dataType = "Integer"),
            @ApiImplicitParam(name = "tagId", value = "栏目id", dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "页数", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页渲染个数", dataType = "Integer"),
    })
    @GetMapping("/myCreate/{uid}")
    public Result myCreate(@PathVariable int uid , int tagId , int page , int pageSize) {
        if (page != 0) {
            page = page * pageSize;
        }
        return new Result(Code.GET_OK , videoService.getCoverByUid(uid, tagId, page, pageSize)) ;
    }

    @ApiOperation(value = "获取我发布的视频tag")
    @GetMapping("/getMyCreateTag/{uid}")
    public Result getMyCreateTag(
            @ApiParam(name = "uid", value = "视频id", required = true)
            @PathVariable int uid) {

       return new Result(Code.GET_OK , videoService.getMyCreateTag(uid)) ;
    }

    @ApiOperation(value = "获取我收藏的视频tag")
    @GetMapping("/getMyFavoriteTag/{uid}")
    public Result getMyFavoriteTag(
            @ApiParam(name = "uid", value = "用户id", required = true)
            @PathVariable int uid
    ) {

        return new Result(Code.GET_OK , videoService.getMyFavoriteTag(uid)) ;
    }

    @ApiOperation(value = "点击量+1")
    @PostMapping("/click/{vid}")
    public Result click(
            @ApiParam(name = "vid", value = "视频id", required = true)
            @PathVariable int vid) {
        int code = videoService.click(vid) ? Code.UPDATE_OK : Code.UPDATE_ERR ;
        return new Result(code) ;
    }

    @ApiOperation(value = "模糊搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "content", value = "key:搜索框value值", dataType = "String"),
            @ApiImplicitParam(name = "tagId", value = "key:栏目id", dataType = "Integer"),
            @ApiImplicitParam(name = "page", value = "key:页数", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "key:每页渲染个数", dataType = "Integer"),
    })
    @PostMapping ("/search")
    public Result search (  @RequestBody JSONObject obj ) {
        String content = obj.getString("content") ;
        int tagId = obj.getByte("tagId") ;
        int page = obj.getByte("page") ;
        int pageSize = obj.getByte("pageSize") ;
        if (page != 0) {
            page = page * pageSize;
        } else {
            if(videoService.search(content,tagId,page,pageSize).isEmpty()) {
                content = "%" ;
                return  new Result(Code.GET_ERR , videoService.search(content,tagId,page,pageSize)) ;
            }
        }
        return new Result(Code.GET_OK , videoService.search(content,tagId,page,pageSize)) ;
    }

    @ApiOperation(value = "获取模糊搜索的tag")
    @PostMapping ("/getSearchTag")
    public Result getSearchTag ( @ApiParam(name = "obj", value = "key:视频id", required = true) @RequestBody JSONObject obj) {
        String content = obj.getString("content") ;
        int code = Code.GET_OK ;
        List<Tag> tags = videoService.searchTagByContent(content) ;
        if(tags.isEmpty()) {
            content = "%" ;
            code = Code.GET_ERR  ;
        }
        return new Result(code, videoService.searchTagByContent(content)) ;
    }

    @ApiOperation(value = "删除父评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "父评论id", dataType = "int"),
            @ApiImplicitParam(name = "obj", value = "key : 视频id", dataType = "JSONObject"),
    })
    @PostMapping("/delFa/{cid}")
    public Result deleteFaCom(  @PathVariable  int cid , @RequestBody JSONObject obj) {
        int vid = obj.getInteger("vid") ;
        int code = commentService.deleteFaCom(cid) &&  videoService.delCom(vid) ? Code.UPDATE_OK : Code.UPDATE_ERR ;
        return new Result(code) ;
    }

    @ApiOperation(value = "删除子评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cid", value = "子评论id", dataType = "int"),
            @ApiImplicitParam(name = "obj", value = "key:视频id", dataType = "JSONObject"),
    })
    @PostMapping("/delCh/{cid}")
    public Result deleteChCom( @PathVariable  int cid ,  @RequestBody JSONObject obj) {
        int vid = obj.getInteger("vid") ;
        int code = commentService.deleteChildCom(cid)  &&  videoService.delCom(vid) ? Code.UPDATE_OK : Code.UPDATE_ERR ;
        return new Result(code) ;
    }

    @ApiOperation(value = "根据id查询用户")
    @GetMapping("/others/{id}")
    public Result getByID(
            @ApiParam(name = "id" , value = "用户id" , required = true)
            @PathVariable Integer id) {
        User user  = userService.fineByID(id) ;
        Integer code = user != null ? Code.GET_OK : Code.GET_ERR ;
        String msg = user != null ? "" : "数据查询失败，请重试 ！" ;
        Map map=new HashMap();
        map.put("username",user.getUsername());
        map.put("id",user.getId());
        map.put("cardCase",user.getCardCase()) ;
        map.put("handUrl",user.getHandUrl()) ;
        map.put("sex" , user.getSex()) ;
        map.put("phone" , user.getPhone()) ;
        String  resObj= JSON.toJSONString(map);
        JSONObject res = JSON.parseObject(resObj);
        return  new Result(code , res , msg) ;
    }

}
